package com.personal.website.lottery.web.controller;

import com.personal.website.lottery.web.common.PageType;
import com.personal.website.lottery.web.model.Page;
import com.personal.website.lottery.web.service.ArticleService;
import com.personal.website.lottery.web.service.DoubleColorBallService;
import com.personal.website.lottery.web.service.PageService;
import com.personal.website.lottery.web.util.TableRecordsGenerate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by Gavin on 2016/2/16.
 */
@Controller
@RequestMapping("/page")
public class PageController {

    @Resource
    private DoubleColorBallService doubleColorBallService;
    @Resource
    private ArticleService articleService;
    @Resource
    private PageService pageService;

    private final Logger logger = LoggerFactory.getLogger(PageController.class);

    @RequestMapping("/chartDes")
    public String chartDes() {
        return "chartDes";
    }

    @RequestMapping("/selectNumber")
    public String selectNumber(Model model) {
        return "selectNumber";
    }

    @RequestMapping("/articles")
    public String articles(HttpServletRequest request, Model model) {
        try {
            String maxArticleSize = request.getParameter("maxArticleSize");
            int maxSize = 30;
            if(maxArticleSize != null && maxArticleSize.trim() != ""){
                maxSize = Integer.parseInt(maxArticleSize);
            }
            if(maxSize <= 0 || maxSize > 100) {
                maxSize = 30;
            }

            model.addAttribute("articles", this.articleService.getArticles(maxSize));
        } catch (Exception e) {
            logger.error("加载文章列表时失败！", e);
        }
        return "articles";
    }

    @RequestMapping("/restForTableData")
    @ResponseBody
    public String restForTableData(HttpServletRequest request){
        String tableName = null;
        try {
            tableName = request.getParameter("tableName");
            String maxChartDataSize = request.getParameter("maxChartDataSize");
            int maxSize = 30;
            if(maxChartDataSize != null && maxChartDataSize != "") {
                maxSize = Integer.parseInt(maxChartDataSize);
            }
            if(maxSize <=0 || maxSize > 100) {
                maxSize = 30;
            }
            TableRecordsGenerate generate = new TableRecordsGenerate(this.doubleColorBallService);
            if("MainTable".equals(tableName)) {
                return generate.generateMainTableRecords(maxSize);
            } else if("BlueTable".equals(tableName)) {
                return generate.generateBlueTableRecords(maxSize);
            } else if("EightDigitTable".equals(tableName)) {
                return generate.generateEightDigitTableRecords(maxSize);
            } else if("ElevenDigitTable".equals(tableName)) {
                return generate.generateElevenDigitTableRecords(maxSize);
            } else if("End3dTable".equals(tableName)) {
                return generate.generateEnd3dTableRecords(maxSize);
            } else if("EndTable".equals(tableName)) {
                return generate.generateEndTableRecords(maxSize);
            } else if("EvenTable".equals(tableName)) {
                return generate.generateEvenTableRecords(maxSize);
            } else if("FourPart012Table".equals(tableName)) {
                return generate.generateFourPart012TableRecords(maxSize);
            } else if("FourPartEnd012Table".equals(tableName)) {
                return generate.generateFourPartEnd012TableRecords(maxSize);
            } else if("OddTable".equals(tableName)) {
                return generate.generateOddTableRecords(maxSize);
            } else {
                logger.error(String.format("加载图表'%s'数据时，表名不存在！", tableName));
                return "";
            }
        } catch (Exception e) {
            logger.error(String.format("加载图表'%s'数据时失败！", tableName), e);
        }
        return "";
    }

    @RequestMapping("/pageShow")
    public String pageShow(HttpServletRequest request, Model model){
        String pageType = request.getParameter("pageType");
        Page page = this.pageService.getPage(pageType);
        model.addAttribute("page", page);
        return "pageShow";
    }

}